package com.timothy.junk.common.parse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;
import com.timothy.junk.R;
import com.timothy.junk.cbond.bean.CBond;

import android.content.Context;
import android.content.res.XmlResourceParser;

public class CBondParser {

	 public static List<CBond> getCBondList(Context context) {
	        List<CBond> cBondList = new ArrayList<CBond>();
	        XmlResourceParser xrp = context.getResources().getXml(R.xml.cbonds);
	        try {
	        	CBond cBond = null;
	            while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
	                if (xrp.getEventType() == XmlResourceParser.START_TAG) {
	                    String tagName = xrp.getName();
	                    if (tagName.equals("cbond")) {
	                    	cBond = new CBond();
	                    }
	                    if(tagName.equals("index") && cBond !=null){
	                    	cBond.setIndex(xrp.nextText());
	                    }
	                    if(tagName.equals("bond_code") && cBond !=null){
	                    	cBond.setBond_code(xrp.nextText());
	                    }
	                    if(tagName.equals("bond_name") && cBond !=null){
	                    	cBond.setBond_name(xrp.nextText());
	                    }
	                    if(tagName.equals("stock_code") && cBond !=null){
	                        cBond.setStock_code(xrp.nextText());
	                    }
	                    if(tagName.equals("trans_price") && cBond !=null){
	                        cBond.setTransPrice(Double.valueOf(xrp.nextText()));
	                    }
	                }
	                
	                if(xrp.getEventType() == XmlResourceParser.END_TAG){
	                    String tagName = xrp.getName();
	                    if (tagName.equals("cbond")) {
	                    	cBondList.add(cBond);
	                    }
	                }
	                xrp.next();
	            }
	        } catch (XmlPullParserException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return cBondList;
	    }
	 
}
